# Makefile for convert2wav project

# 默认配置
IMAGE_NAME = convert2wav
TAG = latest
DOCKER_USER = ayler
REGISTRY = docker.io
DOCKERFILE = Dockerfile

# 帮助信息
.PHONY: help
help:
	@echo "Available commands:"
	@echo "  build         - Build Docker image (no container creation)"
	@echo "  push          - Push image to Docker registry"
	@echo "  tag           - Tag image with custom tag"
	@echo "  clean         - Remove local images"
	@echo "  run           - Run container locally"
	@echo "  stop          - Stop and remove container"
	@echo ""
	@echo "Usage examples:"
	@echo "  make build"
	@echo "  make push"
	@echo "  make push DOCKER_USER=myuser TAG=v1.0.0"

# 构建Docker镜像（仅打包，不创建容器）
.PHONY: build
build:
	@echo "Building Docker image: $(IMAGE_NAME):$(TAG)"
	docker build -f $(DOCKERFILE) -t $(IMAGE_NAME):$(TAG) .
	@echo "Image built successfully: $(IMAGE_NAME):$(TAG)"

# 为镜像添加标签
.PHONY: tag
tag:
	@echo "Tagging image: $(IMAGE_NAME):$(TAG) -> $(REGISTRY)/$(DOCKER_USER)/$(IMAGE_NAME):$(TAG)"
	docker tag $(IMAGE_NAME):$(TAG) $(REGISTRY)/$(DOCKER_USER)/$(IMAGE_NAME):$(TAG)
	@echo "Image tagged successfully"

# 推送镜像到指定用户的仓库
.PHONY: push
push: tag
	@echo "Pushing image to registry: $(REGISTRY)/$(DOCKER_USER)/$(IMAGE_NAME):$(TAG)"
	docker push $(REGISTRY)/$(DOCKER_USER)/$(IMAGE_NAME):$(TAG)
	@echo "Image pushed successfully"

# 构建并推送（完整流程）
.PHONY: build-push
build-push: build push
	@echo "Build and push completed"

# 本地运行容器（用于测试）
.PHONY: run
run:
	@echo "Running container locally..."
	docker run -d --name $(IMAGE_NAME)-test -p 8000:8000 $(IMAGE_NAME):$(TAG)
	@echo "Container started: http://localhost:8000"

# 停止并删除测试容器
.PHONY: stop
stop:
	@echo "Stopping and removing test container..."
	-docker stop $(IMAGE_NAME)-test
	-docker rm $(IMAGE_NAME)-test
	@echo "Test container removed"

# 清理本地镜像
.PHONY: clean
clean:
	@echo "Removing local images..."
	-docker rmi $(IMAGE_NAME):$(TAG)
	-docker rmi $(REGISTRY)/$(DOCKER_USER)/$(IMAGE_NAME):$(TAG)
	@echo "Local images cleaned"

# 查看镜像信息
.PHONY: info
info:
	@echo "Image information:"
	@echo "  Name: $(IMAGE_NAME)"
	@echo "  Tag: $(TAG)"
	@echo "  Full name: $(REGISTRY)/$(DOCKER_USER)/$(IMAGE_NAME):$(TAG)"
	@echo "  Docker user: $(DOCKER_USER)"
	@echo "  Registry: $(REGISTRY)"

# 检查Docker环境
.PHONY: check
check:
	@echo "Checking Docker environment..."
	@docker --version
	@docker info > /dev/null 2>&1 && echo "Docker daemon is running" || echo "Docker daemon is not running"
